const utils = require('../../util/utils');
const userDao = require('../../dao/admin/userDao');
const loginLogService = require("./loginLogService");

var UserService = module.exports;

var genError = function (errCode, message) {
    var error = new Error(message);
    error.errCode = errCode;
    return error;
};

/**
 * 加载用户信息
 * @param name
 * @param username
 * @param offset
 * @param limit
 * @param cb
 */
UserService.getUsers = function(name, username, offset, limit, cb) {
    userDao.getUsers(name, username, offset, limit).then(data => {
        if (!data) {
            throw genError(1, '加载失败，请联系管理员');
        } else {
            return utils.invokeCallback(cb, null, data);
        }
    }).catch(err => {
        console.log('Unable to connect to the database:'+err.stack);
        utils.invokeCallback(cb, err, null);
    });
};

/**
 * 添加用户
 * @param userinfo
 * @param cb
 */
UserService.createUser = function(userinfo, cb) {
    var username = userinfo.user_name;
    userDao.getUserByUserName(username).then(user => {
        if (user) {
            throw genError(1, '用户登录名已被其他用户使用');
        }

        userDao.createUser(userinfo).then(result => {

            utils.invokeCallback(cb, null, result);

        }).catch(function (err) {

            console.log('Unable to connect to the database:'+err.stack);
            utils.invokeCallback(cb, err, null);
        });
    }).catch(err => {

        console.log('Unable to connect to the database:'+err.stack);
        utils.invokeCallback(cb, err, null);
    });
};

/**
 * 用户更新
 * @param updateParams
 * @param cb
 */
UserService.updateUser = function(updateParams, cb) {
    var userId = updateParams.id;
    var username = updateParams.user_name;
    userDao.getUserById(userId).then(async user => {
        if (!user) {
            throw genError(1, '用户不存在');
        }

        var existdUser = await userDao.getUserByUserName(username);
        if (existdUser && existdUser.id !== user.id) {
            throw genError(1, '用户登录名已被其他用户使用');
        }

        userDao.updateUser(updateParams).then(result => {

            utils.invokeCallback(cb, null, result);

        }).catch(function (err) {

            console.log('Unable to connect to the database:'+err.stack);
            utils.invokeCallback(cb, err, null);
        });
    }).catch(err => {

        console.log('Unable to connect to the database:'+err.stack);
        utils.invokeCallback(cb, err, null);
    });
};

/**
 * 密码登录
 * @param signInParams
 * @param callback
 */
UserService.signInByPassword = function (signInParams, cb) {
    var username = signInParams.user_name;
    var password = signInParams.password;
    console.log("username:"+username);
    userDao.getUserByUserName(username).then(async function (user) {
        if (!user) {
            throw genError(1, '用户不存在');
        }

        if (!user.verifyPassword(password)) {
            throw genError(1, '用户名或密码错误');
        }

        utils.invokeCallback(cb, null, user);
    }).catch(err => {

        console.log('Unable to connect to the database:'+err.stack);
        utils.invokeCallback(cb, err, null);
    });
};

/**
 * 删除用户
 * @param ids
 * @param cb
 */
UserService.destroyUsers = function (ids, cb) {
    userDao.destroyUsers(ids).then(result => {
        return utils.invokeCallback(cb, null, result);
    }).catch(err => {
        console.log('Unable to connect to the database:'+err.stack);
        return utils.invokeCallback(cb, {error: 1, msg: '删除失败，请联系管理员'});
    });
};

/**
 * 设置角色
 * @param user_id
 * @param role_ids
 * @param cb
 */
UserService.setUserRoles = function (user_id, role_ids, cb) {
    userDao.setUserRoles(user_id, role_ids).then(result => {
        return utils.invokeCallback(cb, null, result);
    }).catch(err => {
        console.log('Unable to connect to the database:'+err.stack);
        return utils.invokeCallback(cb, {error: 1, msg: '删除失败，请联系管理员'});
    });
};

/**
 * 返回用户角色
 * @param user_id
 * @returns {Promise<*>}
 */
UserService.getUserRoles = async function (user_id) {
    return userDao.getUserRoles(user_id);
};

/**
 * 返回用户可以使用的菜单
 * @param user_id
 * @returns {Promise<*>}
 */
UserService.getMyMenus = async function (user_id) {
    return userDao.getMyMenus(user_id);
};
